package com.health.db.dao.user.impl;
 
import java.util.List;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.user.IUserHospitalRecordDao;
import com.health.db.mapper.UserHospitalRecordMapper;
import com.health.db.model.UserHospitalRecord;
import com.health.db.model.UserHospitalRecordExample;
import com.health.redis.RedisConstants;


public class UserHospitalRecordDaoImpl extends ServiceInterface implements IUserHospitalRecordDao {

	UserHospitalRecordMapper userHospitalRecordMapper;
	String selectByUserId=":userId:";
	String selectByRecId=":recId:";
	String selectByPatientId=":patientId:";
	
	@Override
	public int deleteByPrimaryKey(Long recId) {
		// TODO Auto-generated method stub
		int result=-1;
		UserHospitalRecord record = this.selectByPrimaryKey(recId);
		
		getRedisOperatiorLogic();
		result=userHospitalRecordMapper.deleteByPrimaryKey(recId);
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			String key1 = RedisConstants.UserHospitalRecordMapper+selectByPatientId+record.getPatientId().longValue();
			String key2 = RedisConstants.UserHospitalRecordMapper+selectByRecId+record.getRecId().longValue();
			
			redisOperatiorLogic.deleteObject(key1, getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(key2, getOrderRedisGroupNo("1"));
		}
		
		return result;
	}

	@Override
	public int insert(UserHospitalRecord record) {
		// TODO Auto-generated method stub
		
		int result=-1;
		getRedisOperatiorLogic();
		result=userHospitalRecordMapper.insert(record);
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			String key = RedisConstants.UserHospitalRecordMapper+selectByPatientId+record.getPatientId().longValue();
			
			redisOperatiorLogic.deleteObject(key, getOrderRedisGroupNo("1"));
		}
		return result;
	}

	@Override
	public List<UserHospitalRecord> selectByExample(
			UserHospitalRecordExample example) {
		// TODO Auto-generated method stub
		
		getRedisOperatiorLogic();
		String key =RedisConstants.UserHospitalRecordMapper+selectByPatientId+example.getPatientId().longValue();
		
	/*	if (null!=example.getPatientId()&&example.getPatientId().longValue()!=0) {
			key = key +selectByPatientId +example.getPatientId().longValue();
		}*/
		
		Object object=null;
		List<UserHospitalRecord> list = null;
		if(redisOperatiorLogic.isJedisIsConnected())
		{	
			object = redisOperatiorLogic.readObject(key, getPublicRedisGroupNo("1"));
		 }
	     if(null!=object){
	    	 if (!object.equals("")) {
	    		 list =  (List<UserHospitalRecord>) object;
			}
	     }else{
 
	    	 list=userHospitalRecordMapper.selectByExample(example);
             if(redisOperatiorLogic.isJedisIsConnected())
				{redisOperatiorLogic.setEx(key, list,  getOrderRedisGroupNo("1"));}
		 }
		
		return list;
	}

	@Override
	public UserHospitalRecord selectByPrimaryKey(Long recId) {
		
		getRedisOperatiorLogic();
		String key =RedisConstants.UserHospitalRecordMapper+selectByRecId+recId.longValue();
		
		Object object=null;
		UserHospitalRecord list = null;
		if(redisOperatiorLogic.isJedisIsConnected())
		{	
			object = redisOperatiorLogic.readObject(key, getPublicRedisGroupNo("1"));
		 }
	     if(null!=object){
	    	 if (!object.equals("")) {
	    		 list =  (UserHospitalRecord) object;
			}
	     }else{
 
	    	 list=userHospitalRecordMapper.selectByPrimaryKey(recId);
             if(redisOperatiorLogic.isJedisIsConnected())
				{redisOperatiorLogic.setEx(key, list,  getOrderRedisGroupNo("1"));}
		 }
		
		return list;
	}

	@Override
	public int updateByPrimaryKeySelective(UserHospitalRecord record) {
		// TODO Auto-generated method stub
		int result=-1;
		getRedisOperatiorLogic();
		UserHospitalRecord currrecord = this.selectByPrimaryKey(record.getRecId());
		
		
		result=userHospitalRecordMapper.updateByPrimaryKeySelective(record);
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			
			
			String key1 = RedisConstants.UserHospitalRecordMapper+selectByPatientId+currrecord.getPatientId().longValue();
			String key2 = RedisConstants.UserHospitalRecordMapper+selectByRecId+record.getRecId().longValue();
			
			redisOperatiorLogic.deleteObject(key1, getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(key2, getOrderRedisGroupNo("1"));
		}
		return result;
	}

	public UserHospitalRecordMapper getUserHospitalRecordMapper() {
		return userHospitalRecordMapper;
	}

	public void setUserHospitalRecordMapper(
			UserHospitalRecordMapper userHospitalRecordMapper) {
		this.userHospitalRecordMapper = userHospitalRecordMapper;
	}



	
}
